#include "Main/stdafx.h"
#include "BRNNPlayer.h"
#include "BRNNSoundFun.h"
#include "Games/NiuNiuCardSprite.h"
#include "Games/NiuNiuGameLogic.h"

BRNNPlayer::BRNNPlayer(int iIdex,cocos2d::Node* pSeatNode )
	:GamePlayer(NULL)
	,m_iIdex(iIdex)
	,m_userMoney(0)
	,m_headUrl("")
	,m_pSeatNode(pSeatNode)
{
	init();
}

BRNNPlayer::~BRNNPlayer()
{

}

void BRNNPlayer::init()
{
	m_uPanel = (Node*)m_pSeatNode->getChildByName(StringUtils::format("Sprite_u%d", m_iIdex));
	m_uPanel->setVisible(false);

	m_head = (Sprite*)m_uPanel->getChildByName("Sprite_head");
	//m_userInfo_name = (Text*)m_uPanel->getChildByName("Text_name");
	m_userInfo_money = (Text*)m_uPanel->getChildByName("Text_money");
	m_userInfo_money->setString("0");
	
	PlayerLeave();
}
int  BRNNPlayer::getIdex()
{
	return m_iIdex;
}
void BRNNPlayer::defaultState()
{
	upPlayerState();
}

void BRNNPlayer::startGame()
{
	
}

void BRNNPlayer::EndGame()
{
	if (m_pUserItem == NULL )
	{
		return;
	}
}

void BRNNPlayer::PlayerEnter()
{
	m_uPanel->setVisible(true);

	std::string updateHeadUrl = StringUtils::format("http://dl.rdaohy.com:8888/app/face/get?uid=%d", GetUserID());
	MCWebReq::instance().sendRequestDocumentUrl(updateHeadUrl, nullptr, CC_CALLBACK_1(BRNNPlayer::Rsp_uptHeadUrl_list, this));
}

void BRNNPlayer::PlayerLeave()
{
	defaultState();
	m_headUrl = "";
	m_uPanel->setVisible(false);
}

bool BRNNPlayer::checkUserSitDown()
{
	return m_uPanel->isVisible();
}

void BRNNPlayer::upPlayerInfo()
{
	//m_userInfo_name->setString(GetNickName());
	//m_userInfo_money->setString(StringUtils::format("%lld", (int64) GetUserScore()));
	
// 	if (GetHeadHttp() != "")
// 	{
// 		ImagicDownManager::Instance().addDown(m_head,
// 			GetHeadHttp(),GetUserID());
// 	}
// 	else
// 	{
// 		if (GetGender() == 0)
// 			m_head->setTexture("common/Head0.png");
// 		else
// 			m_head->setTexture("common/Head1.png");
// 	}
}

void BRNNPlayer::showAddGold(int64 iGold)
{
// 	Text* tMoney = (Text*)m_PushPanel->getChildByName("Text_pushMoney");
// 	if(tMoney)
// 		tMoney->setString(StringUtils::format("%lld", iGold));
// 	m_PushPanel->setVisible(true);
}

void BRNNPlayer::upPlayerState()
{
	if (GetUserStatus() == US_READY)
	{
		//m_ready_sprite->setVisible(true);
	}
	if (GetUserStatus() == US_OFFLINE)
	{
		//m_offLine_sprite->setVisible(true);
	}
}

void BRNNPlayer::ClickCard(cocos2d::Node* pCard)
{
	if (pCard == NULL )return;

	int nPosY = pCard->getPositionY();
	if (nPosY == CARD_DWON_POSY)
	{
		pCard->setPositionY(CARD_UP_POSY);
	}
	else
	{
		pCard->setPositionY(CARD_DWON_POSY);
	}
}

void BRNNPlayer::showChatFace(word wItemIndex)
{
	Vec2 endPos = m_uPanel->getPosition();
	Sprite* chatPanel = Sprite::create(StringUtils::format("chat/face%d.png",wItemIndex + 1));
	if (m_iIdex >= 5)
	{
		endPos.x -= m_uPanel->getContentSize().width / 2;
		endPos.x -= chatPanel->getContentSize().width / 2;
	}
	else if (m_iIdex == 0)
	{
		endPos.x += m_uPanel->getContentSize().width / 2;
		endPos.x += chatPanel->getContentSize().width / 2;
		endPos.y += m_uPanel->getContentSize().height / 2;
		endPos.y += chatPanel->getContentSize().height / 2;
	}
	else
	{
		endPos.x += m_uPanel->getContentSize().width / 2;
		endPos.x += chatPanel->getContentSize().width / 2;
	}
	m_pSeatNode->addChild(chatPanel);
	chatPanel->setPosition(endPos);
	chatPanel->runAction(Sequence::create(DelayTime::create(2.0f), RemoveSelf::create(), NULL));
}

void BRNNPlayer::showChatWord(std::string szChatString)
{
	Vec2 endPos = m_uPanel->getPosition();
	Layout* chatPanel = NULL;
	if (m_iIdex >= 5)
	{
		chatPanel = (Layout*)m_pSeatNode->getChildByName("Image_chatRight");
		endPos.x -= m_uPanel->getContentSize().width / 2;
		endPos.x -= chatPanel->getContentSize().width / 2;
	}
	else if (m_iIdex == 0)
	{
		chatPanel = (Layout*)m_pSeatNode->getChildByName("Image_chatLeft");
		endPos.x += m_uPanel->getContentSize().width / 2;
		endPos.x += chatPanel->getContentSize().width / 2;
		endPos.y += m_uPanel->getContentSize().height / 2;
		endPos.y += chatPanel->getContentSize().height / 2;
	}	
	else
	{
		chatPanel = (Layout*)m_pSeatNode->getChildByName("Image_chatLeft");
		endPos.x += m_uPanel->getContentSize().width / 2;
		endPos.x += chatPanel->getContentSize().width / 2;
	}
	Layout* newPanel = (Layout*)chatPanel->clone();
	Text* chatText = (Text*)newPanel->getChildByName("Text_value");
	chatText->setString(szChatString);
	m_pSeatNode->addChild(newPanel);
	newPanel->setPosition(endPos);
	newPanel->runAction(Sequence::create(DelayTime::create(2.0f), RemoveSelf::create(), NULL));
}

void BRNNPlayer::initUserMoney(SCORE money)
{
	m_userMoney = money;
	m_userInfo_money->setString(StringUtils::format("%lld", m_userMoney));
}

void BRNNPlayer::updateUserMoney(SCORE money)
{
	m_userMoney += money;
	m_userInfo_money->setString(StringUtils::format("%lld", m_userMoney));
}

void BRNNPlayer::Rsp_uptHeadUrl_list(const std::string& data)
{
	rapidjson::Document doc;
	doc.Parse<rapidjson::kParseDefaultFlags>(data.c_str());
	if (doc.HasParseError() || !doc.HasMember("face"))
	{
		return;
	}
	std::string strUrl = doc["face"].GetString();
	if (strUrl.find("http") != std::string::npos)
	{
		m_headUrl = strUrl;
		ImagicDownManager::Instance().addDown(m_head,
			strUrl, GetUserID());
	}
	else
	{
		if (GetGender() == 0)
			m_head->setTexture("common/Head0.png");
		else
			m_head->setTexture("common/Head1.png");
	}
}

void BRNNPlayer::showDefaultSound(int itemIndex)
{
	BRNNSoundFun::playEffect(StringUtils::format("default/default%d", itemIndex));
}
